package com.vcc.service.impl;


import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.exception.NotFoundException;
import com.vcc.core.exception.ProgramException;
import com.vcc.core.util.DateUtil;
import com.vcc.core.util.RegexUtil;
import com.vcc.data.entity.Notice;
import com.vcc.data.mapper.NoticeMapper;
import com.vcc.service.boss.request.notice.UpdateNoticeDto;
import com.vcc.service.boss.request.notice.UpdateNoticeStatusDto;
import com.vcc.service.service.NoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.vcc.data.entity.table.NoticeTableDef.NOTICE;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Override
    public Page<Notice> paging(Page<Notice> paging, String title) {
        QueryChain<Notice> queryChain = queryChain();
        if (RegexUtil.checkStringIsNotNull(title)) {
            queryChain.like(Notice::getTitle, title);
        }
        queryChain.eq(Notice::getStatus, true);
        Date date = new Date();
        queryChain.le(Notice::getStartDate, date);
        queryChain.ge(Notice::getEndDate, date);
        queryChain.orderBy(Notice::getCreateDate, false);
        return queryChain.page(paging);
    }

    @Override
    public Page<Notice> paging(Page<Notice> paging, String title, Integer type, Boolean status, Boolean hasPopup) {
        QueryChain<Notice> queryChain = queryChain();
        queryChain.where(NOTICE.TITLE.like(title, StringUtils::isNotBlank))
                .where(NOTICE.TYPE.eq(type, type != null))
                .where(NOTICE.STATUS.eq(status, status != null))
                .where(NOTICE.HAS_POPUP.eq(hasPopup, hasPopup != null));
        queryChain.orderBy(Notice::getCreateDate, false);
        return queryChain.page(paging);
    }

    @Override
    public List<Notice> listPopup() {
        Date date = new Date();
        return queryChain().eq(Notice::getHasPopup, true)
                .eq(Notice::getStatus, true)
                .le(Notice::getStartDate, date)
                .ge(Notice::getEndDate, date)
                .orderBy(Notice::getCreateDate, false)
                .list();
    }

    @Override
    public Long countHasPopup() {
        return queryChain().eq(Notice::getHasPopup, true).eq(Notice::getStatus, true).count();
    }

    @Override
    public Notice getById(Long id) {
        return queryChain().eq(Notice::getId, id).oneOpt().orElseThrow(() -> new NotFoundException("没有当前公告"));
    }

    @Override
    public Boolean updateStatus(UpdateNoticeStatusDto updateNoticeStatusDto) {
        Notice notice = getById(updateNoticeStatusDto.getId());
        //0 上架  true
        //1 下架  false
        if (notice.getStatus().equals(updateNoticeStatusDto.getStatus())) {
            throw new ProgramException("当前状态无需变更");
        }
        return updateChain().set(Notice::getStatus, updateNoticeStatusDto.getStatus()).eq(Notice::getId, notice.getId()).update();
    }

    @Override
    public Boolean updateHasPopup(Long id, Boolean hasPopup) {
        Notice notice = getById(id);
        if (notice.getHasPopup() != null && notice.getHasPopup().equals(hasPopup)) {
            throw new ProgramException("当前状态无需变更");
        }
        return updateChain().set(Notice::getHasPopup, hasPopup).eq(Notice::getId, notice.getId()).update();
    }

    @Override
    public Boolean updateNotice(UpdateNoticeDto updateNoticeDto) {
        Notice notice = getById(updateNoticeDto.getId());
        notice.setTitle(updateNoticeDto.getTitle());
        notice.setContent(updateNoticeDto.getContent());
        notice.setStartDate(DateUtil.parse(updateNoticeDto.getStartDate()));
        notice.setEndDate(DateUtil.parse(updateNoticeDto.getEndDate()));
        notice.setType(updateNoticeDto.getType());
        return updateById(notice);
    }
}
